package com.juzhi.dvrapp.activity;

import android.graphics.Color;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.juzhi.dvrapp.CamApp;
import com.juzhi.dvrapp.R;
import com.juzhi.dvrapp.bean.Device;
import com.juzhi.dvrapp.manager.DeviceManager;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.utils.MLog;
import com.juzhi.dvrapp.widget.SphericalUtil;

import java.util.ArrayList;

public class DeviceMapActivity extends BaseAcitivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Marker marker;
    private Circle circle;
    private String ssid;
    private DeviceManager deviceManager;
    private Device device;

    @Override
    public int getLayoutId() {
        return R.layout.activity_maps;
    }

    @Override
    protected void initView() {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        initHeaderView();
        setTitleText(getResources().getString(R.string.title_activity_maps));
        hideFunction();

        ssid = getIntent().getStringExtra(Config.SSID);
        deviceManager = DeviceManager.getDeviceManager(CamApp.getContext());
        device = deviceManager.getDevice(ssid);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;


        MLog.e(device.getFenceRadius());

        ArrayList<LatLng> triangle = new ArrayList<>();
        triangle.add(new LatLng(22.582200, 113.925992));  // Should match last point
        triangle.add(new LatLng(22.582018, 113.926235));
        triangle.add(new LatLng(22.582000, 113.927231));
        triangle.add(new LatLng(22.581124, 113.927521));
        triangle.add(new LatLng(22.581104, 113.928624));
        triangle.add(new LatLng(22.581024, 113.923521));
        triangle.add(new LatLng(22.581001, 113.922140));  // Should match first point

        mMap.addPolyline(new PolylineOptions()
                .addAll(triangle)
                .color(Color.BLACK));

        double rotation = SphericalUtil.computeAngleBetween(triangle.get(triangle.size() - 1), triangle.get(triangle.size() - 2));
        mMap.addMarker(new MarkerOptions()
                .position(triangle.get(triangle.size()-1))
                .title("车辆位置")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark))
                .anchor((float) 0.5,(float) 0.5)
                .rotation((float) rotation));

        if (device.getLatLng()!=null) {
            //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));  打标记
            mMap.addCircle(new CircleOptions()
                    .center(device.getLatLng())
                    .radius(device.getFenceRadius())
                    .strokeWidth(5)  //圆圈线的宽
                    .strokeColor(getResources().getColor(R.color.colorAccent))   //圆圈线的颜色
                    .fillColor(getResources().getColor(R.color.map_circle_fill))     //圆圈内填充的颜色
            );

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.582200, 113.925992), 15));
        }else {
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.582200, 113.925992), 15));
        }
    }

}
